package com.woniuxy.springboot.configuration;

import com.woniuxy.springboot.interceptor.LoginHandlerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author 佘赛飞
 * @create 2021 -10 -08 11:11 AM
 */
//@Configuration
//public class MyMvcConfiguration implements WebMvcConfigurer {
//    //添加视图控制器，控制视图页面跳转
//    @Override
//    public void addViewControllers(ViewControllerRegistry registry) {
//        //默认转发，thyemleaf默认的前后缀路径有效
//        registry.addViewController("/").setViewName("login");
//        registry.addViewController("/index.html").setViewName("/login.html");
//    }
//}

@Configuration
public class MyMvcConfiguration{
    @Bean
    public WebMvcConfigurer mvcConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("login");
                registry.addViewController("/index.html").setViewName("/login.html");
                registry.addViewController("/main").setViewName("/index.html");
            }
            //对应拦截器路径
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
             registry.addInterceptor(new LoginHandlerInterceptor())
                     //指定拦截的路径
                     .addPathPatterns("/**")
                     //排除特殊路径不拦截
                     .excludePathPatterns("/","/index.html","/user/login","/css/**");
            }
        };
    }
}
